package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/11 15:11
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired(required = false)
    private DishService dishService;

    @Autowired
    private HttpSession session;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        //调用业务添加
        dishService.save(dishDto,session);

        return R.success("添加成功");
    }

    /**
     * 分页查询菜品
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> findByPage(Integer page,Integer pageSize,String name){
        //调用业务层分页数据
        Page<DishDto> dtoPage = dishService.findByPage(page, pageSize, name);

        return R.success(dtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @RequestMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status){
        List<DishDto> dtoList = dishService.findByCategoryId(categoryId, status);

        return R.success(dtoList);
    }
}
